{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputData={}\n",
    "StartLoop = 0\n",
    "nameNotFound = 1\n",
    "posNotFound = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "fw = open(\"MapData.kml\",\"r\")\n",
    "f = open(\"LatLong.txt\",\"w\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:120.520163178378,36.89206629053305,0\n",
      "a2:120.5193894447255,36.89284333308383,0\n",
      "a3:120.5190596853301,36.89239965021736,0\n"
     ]
    }
   ],
   "source": [
    "f.seek(0,0)  #首先把文件指针移动到初始位置\n",
    "dataPoint = f.readlines()\n",
    "for line in dataPoint:\n",
    "    nameIndex = line.find('<Placemark>')\n",
    "    if(nameIndex != -1):#找到了题头\n",
    "        StartLoop = 1\n",
    "        \n",
    "    if(StartLoop):\n",
    "        if(nameNotFound):\n",
    "            nameIndexBegin = line.find('<name>')\n",
    "            nameIndexEnd = line.find('</name>')\n",
    "            if(nameIndexBegin != -1):\n",
    "                name = line[nameIndexBegin+len('<name>'):nameIndexEnd]\n",
    "                nameNotFound = 0\n",
    "        elif((not nameNotFound) and posNotFound):\n",
    "            posIndexBegin = line.find('<coordinates>')\n",
    "            posIndexEnd = line.find('</coordinates>')\n",
    "            if(posIndexBegin != -1):\n",
    "                pos = line[posIndexBegin+len('<coordinates>'):posIndexEnd]\n",
    "                outputData[name]=pos.split(',')\n",
    "                posNotFound = 0\n",
    "        else:\n",
    "            posNotFound = 1\n",
    "            nameNotFound = 1\n",
    "\n",
    "f.close()\n",
    "for item in outputData:\n",
    "    print(item + ':' + outputData[item][0] + ',' + outputData[item][1] + ',' + outputData[item][2])\n",
    "    fw.write(item + ':' + outputData[item][0] + ',' + outputData[item][1] + ',' + outputData[item][2] + '\\n')        \n",
    "fw.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
